package com.brillilab.service.core.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.lab.OrganizationMapper;
import com.brillilab.domain.po.lab.Lab;
import com.brillilab.domain.po.lab.Organization;
import com.brillilab.domain.vo.lab.OrganizationVo;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.lab.IOrganizationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 组织服务
 *
 * @author wmh
 */
@Service
@Transactional
public class OrganizationServiceImpl implements IOrganizationService {
	@Autowired
	private ILabService labService;

	@Autowired
    private OrganizationMapper organizationMapper;

	@Override
	public Organization insert(Organization entity) {
		Integer insert = organizationMapper.insert(entity);
		Assert.isTrue(insert==1, "添加失败");
		return entity;
	}

	@Override
	public boolean deleteById(Long id) {

		 // 判断是否存在子组织
        List<Organization> childOrgs = this.selectChildList(id);
        Assert.isTrue(!CollectionUtils.isEmpty(childOrgs), "存在子级组织");

        //判断组织是否存在关联的实验
        List<Lab> labs=labService.selectList(id);
        Assert.isTrue(!CollectionUtils.isEmpty(labs), "组织下存在关联实验室");

        //删除组织
		Integer b = organizationMapper.deleteById(id);
		return b==1 ? true: false;
	}

	@Override
	@Transactional
	public boolean updateById(Organization org) {
		Integer b = organizationMapper.updateById(org);
		Assert.isTrue(b==1, "修改失败");

        //修改关联实验室的组织名
		List<Lab> labs = labService.selectList(org.getId());
        for (Lab lab : labs){
            lab.setOrgName(org.getName());
            boolean c = labService.updateById(lab);
            Assert.isTrue(c, "修改失败");
        }
		return  true;
	}

	@Override
	public Organization selectById(Long id) {
		return organizationMapper.selectById(id);
	}

	/**
     * 组织列表
     * @param parentId
     * @return
     */
	@Override
    public List<Organization> orgList(Long parentId) {

        LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Organization::getParentId, parentId);
        return organizationMapper.selectList(wrapper);
    }

    /**
     * 查找子级组织
     * @param id
     * @return
     */
    @Override
    public List<Organization> selectChildList(Long id) {
    	LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Organization::getParentId, id);
        return organizationMapper.selectList(wrapper);
    }


	@Override
	public List<OrganizationVo> getOrgList(Long parentId) {
		return organizationMapper.getOrgList(parentId);
	}

	/**
     * 组织分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<Organization> queryPage(String name,int pageNum,int pageSize) {
    	LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>();
    	if(StringUtils.isNotBlank(name)){
			wrapper.like(Organization::getName, name);
		}
    	IPage<Organization> page = organizationMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return new PageVo<Organization> (page);
    }
}
